import 'package:flutter/material.dart';
import 'package:xinxuan/common/appbar_util.dart';
import 'package:xinxuan/common/button_util.dart';
import 'package:xinxuan/common/dialogs/del_dialog.dart';
import 'package:xinxuan/common/goods_order_info_item.dart';
import 'package:xinxuan/common/network_image_util.dart';
import 'package:xinxuan/common/scroll_configuration_common.dart';
import 'package:xinxuan/controllers/my_controller/write_off_controller/write_off_page_controller.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class WriteOffPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return GetBuilder<WriteOffPageController>(
      init: WriteOffPageController(),
      builder: (controller) {
        return Scaffold(
          backgroundColor: pageBgColor,
          appBar: AppBarUtil(
            title: '提货券核销',
            backgroundColor: pageBgColor
          ),
          body: controller.writeoffData.value.id != 0 ? ScrollConfigurationCommon(
            child: ListView(
              children: [
                Container(
                  color: bgWhiteColor,
                  margin: EdgeInsets.only(bottom: 12.h),
                  child: Column(
                    children: [
                      Container(
                        height: 44.h,
                        padding: EdgeInsets.symmetric(horizontal: 16.w),
                        alignment: Alignment.center,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Text(
                              '商品信息',
                              style: TextStyle(
                                color: textBlackColor,
                                fontSize: 14.sp,
                                fontWeight: FontWeight.bold
                              ),
                            )
                          ],
                        ),
                      ),
                      Column(
                        children: controller.writeoffData.value.products!.map((productItem) {
                          return _goodsInfo(
                            productItem.title ?? '', 
                            productItem.image ?? '', 
                            productItem.price ?? '', 
                            productItem.quantity ?? 0
                          );
                        }).toList(),
                      ),
                    ],
                  ),
                ),
                Container(
                  color: bgWhiteColor,
                  padding: EdgeInsets.symmetric(vertical: 8.h),
                  child: Column(
                    children: [
                      GoodsOrderInfoItem(title: '预约门店', value: '${controller.writeoffData.value.store == null ? '门店异常' : controller.writeoffData.value.store!.title}'),
                      GoodsOrderInfoItem(title: '预约时间', value: '${controller.writeoffData.value.pickingAt}'),
                    ],
                  ),
                ),
                controller.writeoffData.value.status == 8 ? Container(
                  padding: EdgeInsets.symmetric(vertical: 16.h),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    color: bgWhiteColor,
                    border: Border(
                      top: BorderSide(
                        width: .5,
                        color: lineColor
                      )
                    )
                  ),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Image.asset(
                        'assets/icons/icon_write_off_success.png',
                        width: 44.w,
                        height: 44.w
                      ),
                      SizedBox(height: 8.h),
                      Text(
                        '核销成功，请与客户确认~',
                        style: TextStyle(
                          color: textBlackColor,
                          fontSize: 14.sp
                        ),
                      )
                    ],
                  ),
                ) : Container()
              ],
            ),
          ) : Container(),
          bottomNavigationBar: controller.writeoffData.value.status != 8 ? BottomAppBar(
            elevation: 0,
            child: Container(
              padding: EdgeInsets.symmetric(horizontal: 12.w),
              height: 56.h,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  Container(
                    width: 120.w,
                    child: ButtonUtil(
                      text: '确认核销', 
                      fontSize: 14.sp, 
                      borderRadius: 36.h, 
                      height: 36.h, 
                      onTop: () {
                        Get.dialog(DelDialog(message: '确认核销吗？', onTap: () {
                          controller.writeoff();
                        }));
                      }
                    ),
                  )
                ],
              ),
            ),
          ) : null,
        );
      },
    );
  }

  /// 商品信息
  Widget _goodsInfo(String title, String headImage, String price, int quantity) {
    return Container(
      padding: EdgeInsets.only(left: 16.w, right: 16.w, bottom: 12.h),
      color: bgWhiteColor,
      child: Row(
        children: [
          NetworkImageUtil(
            url: headImage, 
            width: 90.w, 
            height: 90.w,
            borderRadius: 5.w,
          ),
          SizedBox(width: 12.w),
          Expanded(child: Container(
            constraints: BoxConstraints(
              minHeight: 90.w
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  title,
                  overflow: TextOverflow.ellipsis,
                  maxLines: 2,
                  style: TextStyle(
                    color: textBlackColor,
                    fontSize: 14.sp
                  ),
                ),
                SizedBox(height: 6.h),
                Row(
                  children: [
                  Expanded(child: Text(
                    '¥$price',
                    style: TextStyle(
                      color: mainTextColor,
                      fontSize: 18.sp,
                      fontWeight: FontWeight.bold
                    )
                  )),
                  Text(
                    'x$quantity',
                    style: TextStyle(
                      color: textSubtitleColor,
                      fontSize: 14.sp,
                      fontWeight: FontWeight.bold
                    )
                  ),
                  ],
                )
              ],
            ),
          )),
        ],
      ),
    );
  }
}